<template>
  <div class="ipo_content">
    <div class="ipo_left">
      <div class="header_top">
        <div class="header_item">
          <p class="name">{{ $t('ipo.shenz') }}</p>
          <p class="underline active"></p>
        </div>
      </div>
      <div
        class="list_content"
        @scroll="handleScroll('left')"
        ref="ipoContentLeft"
        v-loading="leftLoading" element-loading-background="#202b2c" element-loading-svg-view-box="-10, -10, 50, 50" :element-loading-spinner="svg"
      >
        <div class="list_item" v-for="(item, index) in stock_list" :key="index">
          <div class="title">
            <p class="name">{{ item.stock_name }}</p>
            <p class="type">US</p>
          </div>
          <p class="name_desc">{{ item.symbol }}</p>
          <div class="list">
            <div class="item">
              <p class="name">{{ $t('ipo.shenjin') }}</p>
              <div class="value proportion_content">
                <div class="proportion_far">
                  <div
                    class="proportion_son"
                    :style="{ width: `${item.subscribe_progress * 100}%` }"
                  ></div>
                </div>
                <p class="proportion_num">
                  {{ item.subscribe_progress * 100 }}%
                </p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.fashi') }}</p>
              <div class="value">
                <p>{{ item.issuance_market_value }}</p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.fajia') }}</p>
              <div class="value">
                <p>{{ item.issuance_price }}</p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.shenjia') }}</p>
              <div class="value">
                <p>{{ item.subscribe_price }}</p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.shouxu') }}</p>
              <div class="value">
                <p>{{ item.fee_rate }}%</p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.peibi') }}</p>
              <div class="value delivery">
                <p class="num">1 : {{ item.allotment_ratio }}</p>
                <span class="num_text">{{ $t('ipo.zhonghou') }}</span>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.keshen') }}</p>
              <div class="value">
                <p class="num">
                  {{ item.min_subscribe_qty }}-{{ item.max_subscribe_qty }}
                </p>
              </div>
            </div>
          </div>
          <div class="list add_margin">
            <div class="item">
              <p class="name">{{ $t('ipo.shenri') }}</p>
              <div class="value">
                <p>{{ item.subscribe_start_date }}</p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.jieri') }}</p>
              <div class="value">
                <p>{{ item.subscribe_end_date }}</p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.chouri') }}</p>
              <div class="value">
                <p>{{ item.lottery_date }}</p>
              </div>
            </div>
            <div class="item">
              <p class="name">{{ $t('ipo.shangri') }}</p>
              <div class="value">
                <p>{{ item.listing_date }}</p>
              </div>
            </div>
          </div>
          <button
            class="Apply_for_purchase"
            @click="handleShowPopu(item)"
            :class="item.max_subscribe_times > item.sum ? 'activeClick' : 'noClick'"
          >
          {{ $t('ipo.shenshen') }}
          </button>
        </div>
      </div>
    </div>
    <div class="ipo_right">
      <div class="header_top">
        <div
          class="header_item"
          v-for="(item, index) in ipoTitle"
          :key="index"
          @click="selectTitle(index)"
        >
          <p class="name">{{ t(item) }}</p>
          <p class="underline" :class="ipoIndex == index ? 'active' : ''"></p>
        </div>
        <!-- <div class="header_item">
          <p class="name">已上市</p>
          <p class="underline"></p>
        </div> -->
      </div>
      
        <div class="list_content">
        <div class="title_top">
          <p class="name name_left">{{ $t('ipo.guming') }}</p>
          <p class="name">{{ $t('ipo.fajia') }}</p>
          <p class="name">{{ $t('ipo.jiage') }}</p>
          <p class="name">{{ $t('ipo.zhangdie') }}</p>
          <p class="name name_end">{{ $t('ipo.shangri') }}</p>
        </div>
        <div class="info_list" v-loading="rightLoading" element-loading-background="#202b2c" element-loading-svg-view-box="-10, -10, 50, 50" :element-loading-spinner="svg">
          <div class="list_item" v-for="(item, index) in stockunlisted">
            <div class="one_left">
              <p class="name">{{ item.stock_name }}</p>
              <div class="text_bottom">
                <p class="type">US</p>
                <span class="info_text">{{ item.symbol }}</span>
              </div>
            </div>
            <div class="one_text">
              <p>{{ item.issuance_price }}</p>
            </div>
            <div class="one_text">
              <p>{{ item.subscribe_price }}</p>
            </div>
            <div class="one_text">
              <p
                :style="
                  isPositive(item.price_change_pct)
                    ? 'color:rgb(25, 202, 124)'
                    : 'color:rgb(254, 85, 79)'
                "
              >
                {{ item.price_change_pct }}%
              </p>
            </div>
            <div class="end_text">
              <p>{{ item.listing_date }}</p>
            </div>
          </div>
        </div>
        <div class="noData" v-if="page==1 && listLength==0 && loadingType == 'noMore'">
        <i class="iconfont icon-meiyougengduo"></i>
        <span class="noData_text">{{ $t('trade.meiyougeng') }}</span>
      </div>
      </div>
      
      </div>
  

    <div v-if="showPopup1028" class="mask1025">
      <div class="popup-container1025">
        <div class="header">
          <div class="header_left">
            <div class="left_bottom">
              <p class="name">{{ stockItem.stock_name }}</p>
              <p class="type">US</p>
            </div>
            <p class="stock_name">{{ stockItem.symbol }}</p>
          </div>
          <CloseBold
            class="close-icon1025"
            style="color: #fff"
            @click="showPopup1028 = false"
          />
        </div>
        <div class="list_content">
          <div class="list_item">
            <p class="key">{{ $t('ipo.shenri') }}</p>
            <p class="name">{{ stockItem.subscribe_start_date }}</p>
          </div>
          <div class="list_item">
            <p class="key">{{ $t('ipo.jieri') }}</p>
            <p class="name">{{ stockItem.subscribe_end_date }}</p>
          </div>
          <div class="list_item">
            <p class="key">{{ $t('ipo.chouri') }}</p>
            <p class="name">{{ stockItem.lottery_date }}</p>
          </div>
          <div class="list_item">
            <p class="key">{{ $t('ipo.shangri') }}</p>
            <p class="name">{{ stockItem.listing_date }}</p>
          </div>
        </div>
        <div class="list_content">
          <div class="list_item">
            <p class="key">{{ $t('ipo.shenjia') }}</p>
            <p class="name">{{ stockItem.subscribe_price }}</p>
          </div>
          <div class="list_item">
            <p class="key">{{ $t('ipo.shenshu') }}</p>
            <div class="input_content">
              <div
                class="remove"
                @click="handleRemove"
                :style="removeDisable ? 'cursor: not-allowed;' : ''"
              >
                <i class="iconfont icon-jianhao"></i>
              </div>
              <input
                type="number"
                name=""
                id=""
                class="input_text"
                v-model="number"
              />
              <div
                class="remove"
                @click="handleAdd"
                :style="addDisable ? 'cursor: not-allowed;' : ''"
              >
                <i class="iconfont icon-jiahao"></i>
              </div>
            </div>
          </div>
        </div>
        <button class="text_btn" @click="handlePostStock">{{ $t('ipo.shenshen') }}</button>
        <div class="text_content">
          <p class="text1">{{ $t('ipo.jiaowen0') }}</p>
          <p class="text1">
            {{ $t('ipo.jiaowen1') }}
            
          </p>
          <p class="text1">
            {{ $t('ipo.jiaowen2') }}
           
          </p>
          <p class="text1">
            {{ $t('ipo.jiaowen3') }}
          
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useI18n } from "vue-i18n"; // 从 vue-i18n 导入 useI18n
import { ref, onMounted, onUnmounted, computed, watch } from "vue";
import loadbottom from "../../components/loadbottom.vue";
import { stockList, addStockList,getstockList } from "../../api/stock.js";
import { throttle } from "lodash";
import {
  isPositive,
  convertToUSFormat,
  convertEnglishDateToChinese,
} from "../../utils/util.js";
import bus from "../../utils/event-bus.js";
const { t, locale } = useI18n();
const svg = `
        <path class="path" d="
          M 30 15
          L 28 17
          M 25.61 25.61
          A 15 15, 0, 0, 1, 15 30
          A 15 15, 0, 1, 1, 27.99 7.5
          L 15 15
        " style="stroke-width: 4px; fill: rgba(0, 0, 0, 0)"/>
      `
const leftLoading = ref(true)
const rightLoading = ref(true)
const stock_list = ref([]);
const showPopup1028 = ref(false);
const stockItem = ref({});
const removeDisable = ref(false);
const addDisable = ref(false);
const number = ref(null);
const stockunlisted = ref([]);
const ipoContentLeft = ref(null);
const page = ref(1);
const listLength = ref(0);
const isScrolling = ref(false);
const ipoTitle = ref(["ipo.daishang", "ipo.yishang"]);
const ipoIndex = ref(0);
const lang = ref("");
const loadingType=ref('loading')
watch(number, (newValue, oldValue) => {
  if (newValue <= stockItem.value.min_subscribe_qty) {
    //  等于最小值时不可再减
    removeDisable.value = true;
  } else {
    removeDisable.value = false;
  }

  if (newValue >= stockItem.value.max_subscribe_qty) {
    //  等于最大值时不可再加
    addDisable.value = true;
  } else {
    addDisable.value = false;
  }
});
onMounted(() => {
  // 获取当前语言
  lang.value = localStorage.getItem("lang");
  // getStockList();
  getLeftstock()
  getStockList("unlisted");

  console.log("数据----", isPositive(-2.1));

  bus.on("data-updated", (data) => {
    // 处理接收到的数据
    lang.value = data;
    if (data == "en") {
      stockunlisted.value.forEach((item) => {
        item.subscribe_start_date = convertToUSFormat(
          item.subscribe_start_date
        );
        item.subscribe_end_date = convertToUSFormat(item.subscribe_end_date);
        item.lottery_date = convertToUSFormat(item.lottery_date);
        item.listing_date = convertToUSFormat(item.listing_date);
      });
      stock_list.value.forEach((item) => {
        item.subscribe_start_date = convertToUSFormat(
          item.subscribe_start_date
        );
        item.subscribe_end_date = convertToUSFormat(item.subscribe_end_date);
        item.lottery_date = convertToUSFormat(item.lottery_date);
        item.listing_date = convertToUSFormat(item.listing_date);
      });
    } else {
      stockunlisted.value.forEach((item) => {
        item.subscribe_start_date = convertEnglishDateToChinese(
          item.subscribe_start_date
        );
        item.subscribe_end_date = convertEnglishDateToChinese(
          item.subscribe_end_date
        );
        item.lottery_date = convertEnglishDateToChinese(item.lottery_date);
        item.listing_date = convertEnglishDateToChinese(item.listing_date);
      });
      stock_list.value.forEach((item) => {
        item.subscribe_start_date = convertEnglishDateToChinese(
          item.subscribe_start_date
        );
        item.subscribe_end_date = convertEnglishDateToChinese(
          item.subscribe_end_date
        );
        item.lottery_date = convertEnglishDateToChinese(item.lottery_date);
        item.listing_date = convertEnglishDateToChinese(item.listing_date);
      });
    }
  });
});
const handleScroll = throttle((type) => {
  //
  // console.log("执行下拉----",type)
  if (type == "left") {
    if (listLength.value == 10) {
      const container = ipoContentLeft.value;
      if (
        container.scrollTop + container.clientHeight >=
        container.scrollHeight - 50
      ) {
        if (!isScrolling.value) {
          isScrolling.value = true;
          // 使用 requestAnimationFrame 优化性能
          requestAnimationFrame(() => {
            page.value++;
            getStockList();
            isScrolling.value = false;
          });
        }
      }
    }
  }
}, 500);
const getLeftstock= async ()=>{
  const data = {
    page: page.value,
  };
  const res = await getstockList(data)
  if (res.code == 200) {
    leftLoading.value = false
    if(lang.value == 'en'){
      res.data.data.forEach((item) => {
        item.subscribe_start_date = convertToUSFormat(
          item.subscribe_start_date
        );
        item.subscribe_end_date = convertToUSFormat(item.subscribe_end_date);
        item.lottery_date = convertToUSFormat(item.lottery_date);
        item.listing_date = convertToUSFormat(item.listing_date);
      });
    }
    if (page.value == 1) {
        stock_list.value = res.data.data;
      } else {
        stock_list.value = stock_list.value.concat(res.data.data);
      }
  }
}
const getStockList = async (type) => {
  const data = {
    page: page.value,
  };
  if (type) {
    data.listing_status = type;
  }
  const res = await stockList(data);
  if (res.code == 200) {
    loadingType.value = 'noMore'
    rightLoading.value = false
    if(lang.value == 'en'){
      res.data.data.forEach((item)=>{
            item.subscribe_start_date = convertToUSFormat(item.subscribe_start_date)
            item.subscribe_end_date = convertToUSFormat(item.subscribe_end_date)
            item.lottery_date = convertToUSFormat(item.lottery_date)
            item.listing_date = convertToUSFormat(item.listing_date)
      })
    }
    listLength.value = res.data.data ? res.data.data.length : 0;
      if (page.value == 1) {
        stockunlisted.value = [];
        stockunlisted.value = res.data.data;
        console.log("这里的变化----", page.value, res.data.data);
      } else {
        stockunlisted.value = stockunlisted.value.concat(res.data.data);
      }

  }
};
const selectTitle = (index) => {
  ipoIndex.value = index;
  page.value = 1;
  if (index == 0) {
    // 待上市
    getStockList("unlisted");
  } else if (index == 1) {
    // 已上市
    getStockList("listed");
  }
};
const handleShowPopu = (item) => {
  if (item.max_subscribe_times > item.sum) {
    stockItem.value = item;
    number.value = JSON.parse(JSON.stringify(item.min_subscribe_qty));
    showPopup1028.value = true;
  }
};
const handlePostStock = async () => {
  const data = {
    subscribe_id: stockItem.value.id,
    subscribe_quantity: number.value,
    winning_amount_due: stockItem.value.subscribe_price + "",
  };
  const res = await addStockList(data);
  if (res.code == 200) {
    ElMessage.success({
      message: res.message,
      type: "success",
      plain: true,
    });
    showPopup1028.value = false
  }
};
const handleRemove = () => {
  if (!removeDisable.value) {
    number.value--;
  }
};
const handleAdd = () => {
  if (!addDisable.value) {
    number.value++;
  }
};
</script>

<style scoped lang="less">
.mask1025 {
  .popup-container1025 {
    width: 600px;
    height: 70%;
    padding: 15px;
    border-radius: 10px;
    overflow:hidden;
    overflow-y:auto;
    .header {
      height: 50px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #3c3e47;
      .header_left {
        .left_bottom {
          display: flex;
          align-items: center;
          .name {
            font-size: 15px;
            color: #fff;
          }
          .type {
            background-color: rgba(0, 123, 255, 0.1);
            color: #007bff;
            border-radius: 3px;
            font-size: 10px;
            padding: 2px 5px;
            box-sizing: border-box;
            border: none;
            font-style: normal;
            margin-left: 10px;
          }
        }
        .stock_name {
          color: #27c77e;
          font-size: 15px;
        }
      }
    }
  }
  .list_content {
    width: 100%;
    padding: 15px 0;
    // border-top: 1px solid #3c3e47;
    border-bottom: 1px solid #3c3e47;
    .list_item {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding-top: 10px;
      color: #fff;
      font-size: 14px;
      .input_content {
        display: flex;
        align-items: center;
        .remove {
          width: 30px;
          height: 25px;
          background-color: rgb(32, 43, 44);
          color: #c8c9cc;
          border-radius: 4px 0 0 4px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }
      }
      .input_text {
        font-size: 11px;
        color: #fff;
        width: 70px;
        height: 25px;
        text-align: center;
        background-color: rgb(32, 43, 44);
        margin: 0 3px;
      }
    }
    .list_item:first-child {
      padding-top: 0px;
    }
  }

  .text_btn {
    width: 100%;
    height: 45px;
    font-size: 14px;
    background-color: #589cf8;
    color: #fff;
    border-radius: 5px;
    margin-bottom: 15px;
    border: none;
  }
  .text_content {
    padding: 0 15px;
    .text1 {
      margin-bottom: 5px;
      color: hsla(0, 0%, 100%, 0.7);
    }
  }
}

.ipo_content {
  width: 100%;
  height: 95%;
  min-height: 95%;
  margin-top: 15px;
  margin-bottom: 15px;
  padding-bottom: 15px;
  display: flex;
  align-items: center;
  .ipo_left {
    width: 780px;
    height: 100%;
    background-color: #202b2c;
    .header_top {
      height: 50px;
      border-bottom: 1px solid #3c3e47;
      display: flex;
      align-items: center;
      padding: 0 15px;
      .header_item {
        position: relative;
        margin-right: 20px;
        cursor: pointer;
        line-height: 35px;
        .name {
          color: rgba(255, 255, 255, 0.6);
          font-size: 14px;
        }
        .underline {
          position: absolute;
          bottom: 0;
          left: 50%;
          width: 20px;
          height: 3px;
          background-color: transparent;
          transition: background-color 0.3s;
          transform: translateX(-50%);
          border-radius: 8px;
        }
        .active {
          background-color: #f5bd08; /* 选中时下划线颜色 */
          border-radius: 8px;
        }
      }
    }
    .list_content {
      width: 100%;
      height: calc(100% - 50px);
      display: flex;
      overflow: hidden;
      overflow-y: auto;
      // align-items: center;
      flex-wrap: wrap;
      justify-content: space-around;
      padding-top: 15px;
      .list_item {
        width: 360px;
        height: auto;
        box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.3);
        border-radius: 10px;
        padding: 15px;
        margin-bottom: 15px;
        background-color: #202b2c;
        color: #fff;
        .title {
          display: flex;
          align-items: center;
          .name {
            font-size: 15px;
            margin-right: 10px;
            color: #fff;
          }
          .type {
            background-color: rgba(0, 123, 255, 0.1);
            color: #007bff;
            border-radius: 3px;
            font-size: 10px;
            padding: 2px 5px;
            box-sizing: border-box;
            border: none;
          }
        }
        .name_desc {
          color: hsla(0, 0%, 100%, 0.7);
          font-size: 14px;
        }
        .list {
          width: 100%;
          padding-bottom: 15px;
          .item {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin-top: 10px;
            .name {
              color: hsla(0, 0%, 100%, 0.7);
              font-size: 12px;
            }
            .value {
              p {
                font-size: 14px;
                color: #fff;
              }
            }
            .delivery {
              display: flex;
              flex-direction: column;
              justify-content: flex-end;
              align-items: flex-end;
            }
            .proportion_content {
              width: 50%;
              display: flex;
              align-items: center;
              .proportion_far {
                border-radius: 50px;
                height: 14px;
                background-color: rgb(238, 238, 238);
                display: inline-flex;
                align-items: center;
                width: 100%;
                .proportion_son {
                  // width: 30%;
                  border-radius: 50px;
                  height: 100%;
                  background-color: rgb(25, 202, 124);
                }
              }
              .proportion_num {
                margin-left: 10px;
              }
            }
          }
        }
        .add_margin {
          margin-top: 10px;
          border-top: 1px solid #3c3e47;
          padding-bottom: 0px;
          .item {
            margin-top: 5px;
          }
        }
        .Apply_for_purchase {
          width: 100%;
          font-weight: 700;
          margin-top: 15px;
          font-size: 12px;
          padding: 4px 0;
          color: #fff;
          border-radius: 5px;
          border: none;
          line-height: 2.5;
          cursor: pointer;
        }
        .activeClick {
          background-color: #589cf8;
        }
        .noClick {
          background-color: #b1b0b0;
          cursor: not-allowed;
          opacity: 0.7;
        }
      }
    }
  }
  .ipo_right {
    width: 100%;
    flex: 1;
    height: 100%;
    background-color: #202b2c;
    margin: 0 15px;
    overflow: hidden;
    overflow-y: auto;
    min-width: 500px;
    .header_top {
      height: 50px;
      border-bottom: 1px solid #3c3e47;
      display: flex;
      align-items: center;
      padding: 0 15px;
      .header_item {
        position: relative;
        margin-right: 20px;
        cursor: pointer;
        line-height: 35px;
        .name {
          color: rgba(255, 255, 255, 0.6);
          font-size: 14px;
        }
        .underline {
          position: absolute;
          bottom: 0;
          left: 50%;
          width: 20px;
          height: 3px;
          background-color: transparent;
          transition: background-color 0.3s;
          transform: translateX(-50%);
          border-radius: 8px;
        }
        .active {
          background-color: #f5bd08; /* 选中时下划线颜色 */
          border-radius: 8px;
        }
      }
    }
    .list_content {
      padding: 15px;
      flex: 1;
      height: calc(100% - 50px);
      overflow: hidden;
      overflow-y: auto;
      background-color: #202b2c;
      .title_top {
        display: flex;
        align-items: center;
        justify-content: space-around;
        .name {
          width: 16%;
          text-align: center;
          font-size: 12px;
          opacity: 0.5;
          color: #fff;
        }
        .name_left {
          width: 36%;
          text-align: left;
        }
        .name_end {
          text-align: right;
        }
      }
      .info_list {
        width: 100%;
        height:calc(100% - 20px);
        .list_item {
          width: 100%;
          display: flex;
          align-items: center;
          margin: 10px 0;
          .one_left {
            width: 36%;
            .name {
              margin-bottom: 5px;
              cursor: pointer;
              font-size: 14px;
              color: #fff;
              margin-bottom: 5px;
            }
            .text_bottom {
              display: flex;
              align-items: center;
              cursor: pointer;
              .type {
                background-color: rgba(0, 123, 255, 0.1);
                color: #007bff;
                border-radius: 3px;
                font-size: 10px;
                padding: 2px 5px;
                box-sizing: border-box;
                border: none;
                font-style: normal;
                margin-right: 5px;
              }
              .info_text {
                color: #fff;
                opacity: 0.6;
                font-size: 14px;
              }
            }
          }
          .one_text {
            width: 16%;
            text-align: center;
            p {
              cursor: pointer;
              font-size: 14px;
              color: #fff;
            }
          }
          .end_text {
            width: 16%;
            text-align: right;
            p {
              cursor: pointer;
              font-size: 14px;
              color: #fff;
            }
          }
        }
      }
    }
    .noData {
      width: 100%;
      height: calc(100% - 50px);
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      .iconfont {
        font-size: 100px;
        color: #fff;
        opacity: 0.7;
      }
      .noData_text {
        font-size: 14px;
        color: #fff;
        opacity: 0.7;
      }
    }
  }
}
</style>
